﻿using System;
using System.Data;
using Maticsoft.DBUtility;//请先添加引用
public partial class _BuildXML : System.Web.UI.Page 
{
    private DataTable TowerDataTable = new DataTable();
    private DataTable DeviceDataTable = new DataTable();
    private String OverLineName = null;
    private void LoadData(String LineName)
    {
        TowerDataTable = DbHelperOra.DataTable("select distinct t.towername,t.towerid,t.longitude,t.latitude,t.elevation,l.linename,l.overlineid from tab_overline l,tab_wire t2,tab_tower t where l.linename = '" + LineName + "' and t2.belongline = l.overlineid and (t.towerid = T2.STARTTOWER or t.towerid = T2.FINISHTOWER) order by l.overlineid , t.towerid asc");

        DeviceDataTable = DbHelperOra.DataTable("select distinct d.deviceid,d.devicename,d.devicetype,d.devicenum,d.belongtower from tab_device d where d.belongtower in (select t.towerid from tab_overline l,tab_wire t2,tab_tower t where l.linename = '" + LineName + "' and t2.belongline = l.overlineid and (t.towerid = T2.STARTTOWER or t.towerid = T2.FINISHTOWER)) order by d.belongtower asc , d.deviceid asc");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        OverLineName = Request.QueryString["taskname"];
        //PrintXML("<?xml version=\"1.0\"?>");
        if (OverLineName != null && OverLineName != "")
        {
            LoadData(OverLineName);
            PrintXMLFromDataTable(TowerDataTable, DeviceDataTable);
        }
    }
    private void PrintXML(String context)
    {
        Response.Write(context);
    }
    private void PrintXMLFromDataTable(DataTable dt_tower, DataTable dt_device)
    {
        
        
        int TowerCount = dt_tower.Rows.Count;
        int DeviceCount = dt_device.Rows.Count;
        if (TowerCount == 0)
        {
            return;
        }
        PrintTask("", true);
        if (TowerCount > 0)
        {
            
            PrintLine(dt_tower.Rows[0][5].ToString(), dt_tower.Rows[0][6].ToString(), true);
            String TowerName = null;
            String TowerID = null;
            String TowerX = null;
            String TowerY = null;
            String TowerZ = null;

            String DeviceName = null;
            String DeviceID = null;
            String DeviceSpecs = null;
            String DeviceAmount = null;
            String DeviceBelongTower = null;

            for (int i = 0; i < TowerCount; ++i)
            {
                TowerName = dt_tower.Rows[i][0].ToString();
                TowerID = dt_tower.Rows[i][1].ToString();
                TowerX = dt_tower.Rows[i][2].ToString();
                TowerY = dt_tower.Rows[i][3].ToString();
                TowerZ = dt_tower.Rows[i][4].ToString();
                if (TowerName != "" && TowerID != "")
                {

                    PrintTower(TowerName, TowerID, TowerX, TowerY, TowerZ, true);
                    for (int j = 0; j < DeviceCount; ++j)
                    {
                        DeviceBelongTower = dt_device.Rows[j][4].ToString();
                        if (DeviceBelongTower == TowerID)
                        {
                            DeviceID = dt_device.Rows[j][0].ToString();
                            DeviceName = dt_device.Rows[j][1].ToString();
                            DeviceSpecs = dt_device.Rows[j][2].ToString();
                            DeviceAmount = dt_device.Rows[j][3].ToString();
                            PrintDevice(DeviceID, DeviceName, DeviceSpecs, DeviceAmount);
                        }
                    }
                    PrintTower(null, null, null, null, null, false);
                }
            }
            PrintLine(null, null, false);
        }
        PrintTask(null, false);
    }
    private void PrintLine(String LineName, String LineID, bool Flag)
    {
        if (Flag)
        {
            PrintXML("<线路 name=\"" + LineName + "\" id=\"" + LineID + "\" action=\"0\">");
        }
        else
        {
            PrintXML("</线路>");
        }
    }
    private void PrintTower(String name, String id, String x, String y, String z, bool Flag)
    {
        if (Flag)
        {
            PrintXML("<杆塔 name=\"" + name + "\" id=\"" + id + "\" x=\"" + x + "\" y=\"" + y + "\" z=\"" + z + "\" action=\"0\">");
        }
        else
        {
            PrintXML("</杆塔>");
        }
    }
    private void PrintDevice(String id, String name, String specs, String amount)
    {
        PrintXML("<设备 id=\"" + id + "\" name=\"" + name + "\" specs=\"" + specs + "\" amount=\"" + amount + "\" action=\"0\"/>");
    }
    private void PrintTask(String TaskName, bool Flag)
    {
        if (Flag)
        {
     
            PrintXML("<任务 name=\"" + TaskName + "\">");
        }
        else
        {
            PrintXML("</任务>");
        }

    }
}
